
#include "gt_mongo.h"

GDALMongo::GDALMongo()
{
}


GDALMongo::~GDALMongo()
{
}

GDALMongo* GDALMongo::GetInstance()
{
	return new GDALMongo();
}

GDALMongoConn* GDALMongo::GetMongoConn(string host,string db,string user,string pwd)
{
	for (vector<GDALMongoConn*>::iterator it = conns.begin(); it != conns.end(); it++)
	{
		if ((*it)->host == host && (*it)->db == db)
		{
			return (*it);
		}
	}
	GDALMongoConn* conn =new GDALMongoConn();
	conn->host = host;
	conn->db = db;
	conn->user = user;
	conn->pwd = pwd;
	conn->isAuthed = false;
	conn->isConned = false;

	client::initialize();
	DBClientConnection* mongoConn =new DBClientConnection();
	try
	{
		mongoConn->connect(host);
		conn->isConned = true;
	}
	catch (const std::exception&)
	{
	}
	string errmsg = "auth success";
	bool auth = mongoConn->auth(db, user, pwd, errmsg);
	if (auth)
	{
		conn->isAuthed = true;
	}
	if (conn->isConned&&conn->isAuthed)
	{
		conn->conn = mongoConn;
		conns.push_back(conn);
	}
	return conn;
}